//
//  ViewController.m
//  30_UIViewAnimation
//
//  Created by 徐昊 on 16/8/22.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Frame.h"

@interface ViewController ()
@property (nonatomic, strong) UIView *animationView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.animationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    _animationView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:_animationView];
    
    // 1. UIViewAnimation 方法
    // 1> 开始动画设置
    // 参数1: 动画标识
    // 参数2: 上下文
    [UIView beginAnimations:nil context:NULL];
    /** 
     * UIViewAnimationCurveEaseInOut 开始和结束慢
     * UIViewAnimationCurveEaseIn 开始慢
     * UIViewAnimationCurveEaseOut 结束慢
     * UIViewAnimationCurveLinear 匀速
     */
    // 2> 设置动画曲线, 如何进行动画
    [UIView setAnimationCurve:UIViewAnimationCurveLinear];
    // 3> 设置动画持续的时间
    [UIView setAnimationDuration:3];
    // 4> 动画延时 10s
//    [UIView setAnimationDelay:10];
//    [UIView setAnimationStartDate:[NSDate dateWithTimeIntervalSinceNow:10]];
    // 5> 动画重复次数
    [UIView setAnimationRepeatCount:2.5];
    // 6> 设置往返动画, 一个来回算一次
//    [UIView setAnimationRepeatAutoreverses:YES];
    // 忽略动画过程
    [UIView setAnimationsEnabled:YES];
    // 7> 动画的改变
//    _animationView.h_width = self.view.h_width;
    // 8> 动画的代理, 不签订代理, 代理方法不执行(默认为不签订)
    [UIView setAnimationDelegate:self];
    //  将要开始
    [UIView setAnimationWillStartSelector:@selector(animationWillStart:context:)];
    //  已经结束
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    // 9> 结束动画设置, 执行该动画
    [UIView commitAnimations];
    
    // 2. UIViewAnimationBlock
    // 1> UIViewAnimation
    // 参数1: 持续时间
    // 参数2: block, 需要执行的动画效果
//    [UIView animateWithDuration:5 animations:^{
//        _animationView.h_x = 1;
//    }];
    // 2>
    // 参数3: 完成后, 需要执行的操作
//    [UIView animateWithDuration:5 animations:^{
//        
//        _animationView.h_x = 1;
//    } completion:^(BOOL finished) {
//        NSLog(@"动画完成后, 需要执行的操作");
//        
//    }];
    // 3>
    // 参数3: 选项枚举, 按位枚举 1 << 0, 多选
    [UIView animateWithDuration:5 delay:5 options:UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse animations:^{
        
        _animationView.h_x = 300;
    } completion:^(BOOL finished) {
        
        NSLog(@"动画完成后, 所做的操作");
    }];
}
- (void)animationWillStart:(NSString *)animationID context:(void *)context {
    NSLog(@"动画将要开始");
}
- (void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
    NSLog(@"动画已经结束");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
